package goComfyuiApi

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type PromptResp struct {
	ExecInfo struct {
		QueueRemaining int `json:"queue_remaining"`
	} `json:"exec_info"`
}

// 获取剩余队列数
func ApiPrompt(comfyuiUrl string) (*PromptResp, error) {
	url := genComfyUIApiUrl(comfyuiUrl, "prompt")
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read ApiPrompt response: %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("ApiPrompt request failed: %s", string(body))
	}

	var re PromptResp
	err = json.Unmarshal(body, &re)
	if err != nil {
		return nil, fmt.Errorf("failed to decode ApiPrompt response: %v", err)
	}

	return &re, nil
}
